<template>
	<view class="">
		<NavBar title="评价" :isShowLeftBtn="true">
		</NavBar>
	</view>
	<view class="content" :style="{height:contentHeight}">
		<view class="main" :style="{height:contentHeight}">
			<scroll-view scroll-y="true" :style="{height: contentHeight}" :refresher-threshold="100"
				@scrolltolower="scrolltolowerFun" refresher-enabled="true" refresher-default-style="none">
				<view class="main_list">
					<view class="main_item" v-for="(item,index) in evaluateList" :key="index">
						<view class="flex m_b_20">
							<view class="m_r_14">
								<up-image :show-loading="true" :src="item.avatar" width="64rpx"
									height="64rpx" shape="circle"></up-image>
							</view>
							<view class="">
								<view class="">
									<text class="f_24_0 u-line-1" style="color: #181818;">
										{{item.nickname}}
									</text>
								</view>
								<view class="">
									<text class="f_22_0" style="color: #B1B1B2;">
										{{item.create_time}}
										<!-- 时间没有做判断 -->
									</text>
								</view>
							</view>
						</view>
						<view class="m_b_26">
							<text class="f_24_0" style="color: #4B4B4B;">
								{{item.evaluate_remark}}
							</text>
						</view>
						<view class="main_item_img flex m_b_4">
							<view class="m_r_20 m_b_20" v-for="(item2,index2) in item.imgList" :key="index2">
								<up-image :show-loading="true" :src="item2" width="224rpx" height="204rpx"></up-image>
							</view>
						</view>
						<view class="main_item_other_pj m_b_12" style="">
							<view class="main_item_other_pj_item m_b_20" v-for="(item3,index3) in item.reviewList"
								:key="index3">
								<text class=" f_22_0" style="color: #7F7F7F;">
									{{item3.nickname}}：{{item3.content}}
								</text>
							</view>
						</view>
						<view class="flex" style="justify-content: flex-end;">
							<view class="flex m_r_28" @click="showPopup(index)">
								<view class="m_r_8 m_t_4">
									<up-icon name="/static/images/index/xx_icon.png" size="12"
										color="#C5C5C5"></up-icon>
								</view>
								<view class="">
									<text class="f_24_0" style="color: #181818;">
										评论
									</text>
								</view>
							</view>
							<view class="flex m_t_4">
								<view class="m_r_8">
									<view class="" v-if="item.is_like" @click="onDz(item,index,0)">
										<up-icon name="thumb-up" size="18" color="#F44816"></up-icon>
									</view>
									<view class="" v-else @click="onDz(item,index,1)">
										<up-icon name="thumb-up" size="18" color="#4D4D4D"></up-icon>
									</view>
								</view>
								<view class="f_24_0" style="color: #4D4D4D;">
									{{item.like_num}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="empty_box" v-if="!loadShow &&evaluateList.length ==  0">
					<view class="">
						<up-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png">
						</up-empty>
					</view>
				</view>
				<view class="loading_box m_t_32" v-if="loadShow">
					{{ loadMessage }}
				</view>
			</scroll-view>
		</view>
		<view class="">
			<up-popup :show="popupShow" mode="bottom" round="10" :safeAreaInsetBottom="true">
				<view class="popup_box">
					<view class="popup_title m_b_46">
						<text class="f_30_0_b">
							评论
						</text>
					</view>
					<view class="popup_close" @click="onPopupClose">
						<up-icon name="close" color="#000000" size="17"></up-icon>
					</view>
					<view class="m_b_80">

						<up-textarea v-model="pjVal" placeholder="请输入" height="140"></up-textarea>
					</view>
					<view class="popup_btn" @click="onPopupOk">
						<view class="m_b_10">
							<text class="f_24_f">
								评论
							</text>
						</view>
					</view>
				</view>
			</up-popup>
		</view>
	</view>
</template>
<script setup lang="ts">
	import { ref, reactive, getCurrentInstance } from "vue";
	import { onLoad, onShow } from "@dcloudio/uni-app";
	import NavBar from "@/components/navbar.vue"
	const { proxy } = getCurrentInstance()
	const contentHeight = ref('100vh')
	const statusBarHeight = ref(88)
	const sId = ref(0)
	onLoad((e) => {
		uni.getSystemInfo({
			success: function (info) {
				statusBarHeight.value = (info.statusBarHeight + 44) * 2
				contentHeight.value = `calc(100vh - ${statusBarHeight.value + 76}rpx)`
			}
		});
		sId.value = e.id
		getEvaluate()
	})
	// 审核消息列表
	const page = ref(1)
	const loadShow = ref(false)
	const loadMessage = ref('加载中')
	const scrolltolowerFun = () => {
		if (loadShow.value) return
		page.value = ++page.value;
		loadShow.value = true;
		setTimeout(() => {
			getEvaluate()
		}, 1000)
	}
	// 评价列表 取前两条
	const evaluateList = ref([])
	const getEvaluate = async () => {
		if (loadMessage.value == '没有更多了') {
			loadShow.value = true;
			setTimeout(() => {
				loadShow.value = false;
			}, 200)
			return
		}
		let res = await proxy.$http('index.getEvaluate', { shop_id: sId.value, page: page.value })
		let data = res.data.data
		if (res.code == 1) {
			if (data.length == 0) {
				loadMessage.value = "没有更多了"
			} else {
				evaluateList.value.push(...data)
			}
		} else {
			loadMessage.value = "加载出错"
		}
		setTimeout(() => {
			loadShow.value = false;
		}, 200)
	}
	// 点赞评价
	const onDz = async (e, index, type) => {
		let res = await proxy.$http('index.evaluateLike', { evaluate_id: e.evaluate_id, type: type })
		evaluateList.value[index].is_like = type
		if (type == 1) {
			evaluateList.value[index].like_num += 1
		} else {
			evaluateList.value[index].like_num -= 1
		}
	}
	// 评价
	const popupShow = ref(false)
	const activeIdx = ref(0)
	const pjVal = ref()
	const showPopup = (e) => {
		pjVal.value = ''
		activeIdx.value = e
		popupShow.value = true
	}
	const onPopupClose = () => {
		popupShow.value = false
	}
	const onPopupOk = () => {
		popupShow.value = false
		evaluateReview()
	}

	const evaluateReview = async () => {
		let res = await proxy.$http('index.evaluateReview', { evaluate_id: evaluateList.value[activeIdx.value].evaluate_id, content: pjVal.value })
		if (res.code == 1) {
			uni.$u.toast('已评论')
			setTimeout(() => {
				loadMessage.value = "加载中"
				page.value = 1
				evaluateList.value = []
				getEvaluate()
			}, 800)
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		background: #F7F8FC;
		padding: 20rpx;
		padding-bottom: 64rpx;

		.main_list {
			background: #ffffff;

			.main_item {
				padding: 34rpx 20rpx;
				padding-bottom: 30rpx;
				border-top: 2rpx solid #F3F3F1;

				.main_item_other_pj {
					.main_item_other_pj_item {
						width: 631rpx;
						padding: 18rpx 24rpx;
						background: #F3F3F3;
						border-radius: 28rpx;
					}
				}
			}
		}
	}

	.popup_box {
		padding: 46rpx;
		padding-top: 36rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		position: relative;

		.popup_title {
			text-align: center;
		}

		.popup_close {
			position: absolute;
			top: 18rpx;
			right: 18rpx;
			padding: 6rpx;
		}

		.popup_btn {
			margin: 0 auto;
			height: 83rpx;
			background: #F93F1A;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}

	::v-deep.u-textarea__field {
		font-size: 24rpx !important;
	}
</style>